package com.sann.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sann.common.utils.ExceptionUtil;
import com.sann.common.utils.R;
import com.sann.eduservice.entity.teacher.Teacher;
import com.sann.eduservice.entity.teacher.vo.TeacherQuery;
import com.sann.eduservice.service.TeacherService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author zjw
 * @since 2021-03-09
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher/")
@Slf4j
@CrossOrigin //跨域
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAll(){
        try {
//            int result = 10/0;
        }catch (Exception e){
            e.printStackTrace();
            log.error("出异常了=====>{}", ExceptionUtil.getMessage(e));
        }
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")
    public R removeTeacher(@ApiParam(name="id",value = "讲师ID",required = true) @PathVariable String id){
        boolean result = teacherService.removeById(id);
        if (result) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    // 3 分页查询讲师的方法
    @ApiOperation(value ="分页查询讲师")
    @GetMapping("pageTeacher/{pageNo}/{pageSize}")
    public R pageListTeacher(@PathVariable long pageNo,
                             @PathVariable long pageSize) {
        //创建page对象
        Page<Teacher> eduTeacherPage = new Page<>(pageNo, pageSize);

        //调用方法实现分页
        //调用方法时候，底层封装，把分页所有数据封装到 eduTeacherPage 对象里面
        Page<Teacher> page = this.teacherService.page(eduTeacherPage);

        Long total = page.getTotal();// 总记录数
        List<Teacher> records = page.getRecords();// 数据list集合
        return R.ok().data("total", total).data("rows", records);
    }

    /**
     * @RequestBody 请求数据封装成json数据格式放到请求体中，此种方式只能是POST请求方式
     * @ResponseBody 返回json格式数据，@RestController已经标注在该类上了，这里可以不用写@ResponseBody
     * @return
     */
    // 4 条件查询带分页的方法
    @ApiOperation(value="带条件分页查询讲师")
    @RequestMapping(value = "pageTeacherCondition/{pageNo}/{pageSize}",method = RequestMethod.POST)
    @ResponseBody
    public R pageTeacherCondition(@PathVariable long pageNo,
                                  @PathVariable long pageSize,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        Page<Teacher> eduTeacherPage = new Page<>(pageNo, pageSize);

        // 构建条件
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }
        wrapper.orderByDesc("gmt_create");

        Page<Teacher> page = this.teacherService.page(eduTeacherPage, wrapper);

        long total = page.getTotal();
        List<Teacher> records = page.getRecords();
        return R.ok().data("total", total).data("rows", records);
    }

    //5 添加讲师接口的方法
    @ApiOperation(value="添加讲师")
    @PostMapping(value="addTeacher")
    public R addTeacher(@RequestBody Teacher eduTeacher){
        boolean save = this.teacherService.save(eduTeacher);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    //6 根据讲师id进行查询
    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping(value="getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        Teacher eduTeacher = this.teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    //讲师修改功能
    @ApiOperation(value="修改讲师")
    @PostMapping(value="updateTeacher")
    public R updateTeacher(@RequestBody Teacher eduTeacher){
        boolean flag = this.teacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

}

